package web.common;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

import java.util.Map;
import java.util.HashMap;
import java.io.File;
import java.io.FileOutputStream;
import java.io.Writer;
import java.io.OutputStreamWriter;

import org.dom4j.Document;
import org.dom4j.XPath;
import org.dom4j.DocumentHelper;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;

/**
 * Created by IntelliJ IDEA.
 * User: duanbin
 * Date: 2007-8-21
 * Time: 22:30:13
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseModifier {
    protected final Logger logger = Logger.getLogger(this.getClass());
    protected Document doc;

    public BaseModifier() {
        try {
            String fileName = getFileName();
            logger.debug("Loading File: " + fileName + " to modify.");
            doc = XmlAccessUtil.getXmlDoc(fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected abstract String getFileName();
    protected abstract void doModify(String name);

    public void modify(String name){
         doModify(name);
         XmlAccessUtil.modifyXmlDoc(doc,getFileName());
    }

    protected XPath getXPath4DefaultNS(String xpath_exp){
        Map nsMap = new HashMap();
        nsMap.put("ns","http://www.springframework.org/schema/beans");  //handle default namespace
        XPath xPath = DocumentHelper.createXPath(xpath_exp);
        xPath.setNamespaceURIs(nsMap);
        return xPath;
    }

    protected String uncapFirst(String s){
        return s.substring(0,1).toLowerCase() + s.substring(1);
    }
}
